package com.cyzy.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cyzy.dto.Result;
import com.cyzy.pojo.ErrorProblem;
import com.cyzy.service.ErrorProblemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author ll
 * @version 1.0
 * @description: TODO
 * @date 2025/5/22 下午4:44
 */
@RestController
@RequestMapping("/error")
public class ErrorProblemController {
    //RESTful接口规范：查询用get  增加用post  修改用put  删除用delect
    //登录用post
    @Autowired
    private ErrorProblemService errorProblemService;


    @GetMapping("/page")
    public Result getErrorAll(ErrorProblem errorProblem){
        // 启动分页，page表示当前页，size表示每页的记录数
        System.out.println(errorProblem);
        PageHelper.startPage(errorProblem.getPage(), errorProblem.getPageSize());
        List<ErrorProblem> errorList = errorProblemService.getAllError(errorProblem);
        if(errorList == null){
            return Result.error().message("查询失败");
        }
        PageInfo<ErrorProblem> pageInfo = new PageInfo<>(errorList);
        return Result.ok().data("errorList",pageInfo).data("count",pageInfo.getTotal());
    }
    //新增异常问题
    @PostMapping("/addError")
    public Result saveError(@RequestBody ErrorProblem errorProblem) {
        boolean save = errorProblemService.save(errorProblem);
        if (save){
            return Result.ok().message("新增成功");
        }

        return Result.error().message("失败");
    }
    //通过ID修改异常问题
    @PutMapping("/updateError")
    public Result updateUser(@RequestBody ErrorProblem errorProblem) {
//        System.out.println(errorProblem);
        boolean update = false;
        if(errorProblem.getErrorResult() == null){
            update = errorProblemService.updateById(errorProblem);
        }else{
            Date nowTime = new Date();
            errorProblem.setErrorSolvingTime(nowTime);
            System.out.println(errorProblem+"-------------------65");
            update = errorProblemService.updateById(errorProblem);
            System.out.println(errorProblem);
        }
        if(update){
            return Result.ok().message("修改成功");
        }
        return Result.error().message("失败");
    }
    //删除一个
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam("errorId") Integer errorId){
        System.out.println(errorId);
        LambdaUpdateWrapper<ErrorProblem> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(ErrorProblem::getErrorStatus, 2) // 设置 errorStatus=2
                .eq(ErrorProblem::getErrorId, errorId); // 匹配指定 ID
        // 2. 执行更新
        boolean updateStatus = errorProblemService.update(updateWrapper);
        if(updateStatus){
            return Result.ok().message("删除成功");
        }
        return Result.error().message("失败");
    }
    //批量假删
    @DeleteMapping("/deleteBatch")
    public Result delete(@RequestParam List<Integer> errorIds){
        // 1. 构造更新条件：将 errorStatus 设为 2，且 ID 在 errorProblemIds 中
        LambdaUpdateWrapper<ErrorProblem> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(ErrorProblem::getErrorStatus, 2)
                .in(ErrorProblem::getErrorId, errorIds);
        // 2. 批量更新
        boolean update = errorProblemService.update(updateWrapper);
        if(update){
            return Result.ok().message("删除成功");
        }
        return Result.error().message("失败");
    }
}
